import WebpackLicense from '@components/WebpackLicense';

<WebpackLicense from="https://webpack.js.org/configuration/node/" />

# Node

The following Node.js options configure whether to polyfill or mock certain [Node.js globals](https://nodejs.org/docs/latest/api/globals.html).

## node.global

- **Type:** `boolean | 'warn'`
- **Default:** `'warn'`

Controls whether Rspack should provide a polyfill for the Node.js `global` object when bundling for non-Node environments.

See [the Node.js documentation](https://nodejs.org/api/globals.html#globals_global) for the exact behavior of this object.

Optional values:

- `true`: Rspack injects a polyfill so that `global` is available in the bundle. This ensures compatibility with code that relies on Node.js globals in non-Node runtimes.
- `false`: No polyfill is provided. References to `global` remain untouched. If your target environment does not define `global`, accessing it will throw a `ReferenceError` at runtime.
- `'warn'`: Inject a polyfill like `true`, but also emit a warning when `global` is used.

For example, to disable `global` polyfill:

```js title="rspack.config.mjs"
export default {
  node: {
    global: false,
  },
};
```

## node.\_\_filename

- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
- **Default:** `'warn-mock'`, `'node-module'` when [output.module](/config/output#outputmodule) is enabled

Controls how Rspack handles the Node.js [`__filename`](https://nodejs.org/api/modules.html#__filename) variable when bundling for non-Node environments.

Optional values:

- `true`: The filename of the input file relative to the [`context`](/config/context) option.
- `false`: Rspack won't touch your `__filename` code, which means you have the regular Node.js `__filename` behavior. The filename of the **output** file when run in a Node.js environment.
- `'mock'`: The fixed value `'/index.js'`.
- `'warn-mock'`: Use the fixed value of `'/index.js'` but show a warning.
- `'node-module'`: Replace `__filename` in CommonJS modules to `fileURLToPath(import.meta.url)` when `output.module` is enabled.
- `'eval-only'`: Equivalent to `false`.

For example, to leave `__filename` as it is:

```js title="rspack.config.mjs"
export default {
  node: {
    __filename: false,
  },
};
```

## node.\_\_dirname

- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
- **Default:** `'warn-mock'`, `'node-module'` when `output.module` is enabled

Controls how Rspack handles the Node.js [`__dirname`](https://nodejs.org/api/modules.html#__dirname) variable when bundling for non-Node environments.

Optional values:

- `true`: The dirname of the **input** file relative to the [`context`](/config/context) option.
- `false`: Rspack won't touch your `__dirname` code, which means you have the regular Node.js `__dirname` behavior. The dirname of the **output** file when run in a Node.js environment.
- `'mock'`: The fixed value `'/'`.
- `'warn-mock'`: Use the fixed value of `'/'` but show a warning.
- `'node-module'`: Replace `__dirname` in CommonJS modules to `fileURLToPath(import.meta.url + "/..")` when `output.module` is enabled.
- `'eval-only'`: Equivalent to `false`.

For example, to leave `__dirname` as it is:

```js title="rspack.config.mjs"
export default {
  node: {
    __dirname: false,
  },
};
```
